package com.sairui.xiaoruilibdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import com.sairui.xiaoruilib.XiaoRuiActivity;
import com.sairui.xiaoruilib.XiaoRuiFragment;
import com.sairui.xiaoruilib.widgets.WXBottomTouchView;
import com.sairui.xiroruilib.recognize.VoiceRecognizeManager;
import com.sairui.xiroruilib.speak.SpeakManager;

public class MainActivity extends AppCompatActivity {
    private MyViewPager mContent;

    private WXBottomTouchView mWXBottomTouchView;
    private MyViewPagerAdapter mMyViewPagerAdapter;

    private int mPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContent = findViewById(R.id.content);

        //初始化语音识别和语音合成
        SpeakManager.getInstance().init("11226642",
                "jtwTBFTuZezBRr5cgPnUNCWf","6Aey6KaHlxrObCPTmkYNgxVDDFAqx0km",this);
        VoiceRecognizeManager.getInstance().init("11226642",
                "jtwTBFTuZezBRr5cgPnUNCWf","6Aey6KaHlxrObCPTmkYNgxVDDFAqx0km",this);

        mMyViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
        mContent.setAdapter(mMyViewPagerAdapter);
        mContent.setOffscreenPageLimit(2);
        mContent.setCurrentItem(0);

        mWXBottomTouchView = findViewById(R.id.voice_input);
        mWXBottomTouchView.setTouchListener(new WXBottomTouchView.TouchListener() {
            @Override
            public void start_recognize() {
                ((XiaoRuiFragment)mMyViewPagerAdapter.getItem(2)).start_recognize();
            }

            @Override
            public void cancel_recognize() {
                ((XiaoRuiFragment)mMyViewPagerAdapter.getItem(2)).cancel_recognize();
            }

            @Override
            public void end_recognize() {}

            @Override
            public void actionUp() {
                ((XiaoRuiFragment)mMyViewPagerAdapter.getItem(2)).actionUp();
            }
        });
        mWXBottomTouchView.setWxClickListener(new WXBottomTouchView.WxClickListener() {
            @Override
            public void click() {
                mContent.setCurrentItem(2);
            }
        });

        BottomManager mBottomManager = new BottomManager(this,
                new int[]{R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round},
                new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher},
                new String[]{"首页", "标签2", "标签3", "标签4"}, R.color.main_black, R.color.time_color
        );
        mBottomManager.setSelectListener(new BottomManager.SelectListener() {
            @Override
            public void clickPosition(int position) {
                ((XiaoRuiFragment)mMyViewPagerAdapter.getItem(2)).dismissPop();
                mPosition = position;
                switch (position) {
                    case 0:
                        mContent.setCurrentItem(0);
                        break;
                    case 1:
                        mContent.setCurrentItem(1);
                        break;
                    case 2:
                        mContent.setCurrentItem(3);
                        break;
                    case 3:
                        mContent.setCurrentItem(4);
                        break;
                }
            }
        });
        mBottomManager.setCurrentPosition(mPosition);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SpeakManager.getInstance().release();
        VoiceRecognizeManager.getInstance().release();
    }


}
